﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Dude_Platformer.Weapon
{

    public enum GunType
    {
        MAVERICK,
        STINGER,
        SUPER,
        ENNEMY_GUN
    }

    /// <summary>
    /// allow to summon ammo.
    /// </summary>
    public class Gun
    {
        private GunType type;
        /// <summary>
        /// recovery time in ms.
        /// </summary>
        private Double recoverTime;
        private Double reloadTime;
        private Int32 bulletDamage;
        private DateTime lastShoot;
        private String spriteSet;
        private Int16 maxAmmo;

        private Int32 remainingAmmo;
        public Int32 RemainingAmmo { get { return remainingAmmo; } }

        public void addAmmo(Int32 ammo)
        {
            remainingAmmo += ammo;
        }

        public Gun(GunType type)
        {
            updateType(type);
        }

        public void updateType(GunType type)
        {

            switch (type)
            {
                case GunType.ENNEMY_GUN:
                    recoverTime = 500;
                    reloadTime = 1000;
                    bulletDamage = 1;
                    maxAmmo = short.MaxValue;
                    spriteSet = "enemyBullet";
                    break;
                case GunType.STINGER:
                    recoverTime = 500;
                    reloadTime = 1000;
                    bulletDamage = 1;
                    maxAmmo = 50;
                    spriteSet = "bullet";
                    break;
                case GunType.MAVERICK:
                    recoverTime = 300;
                    reloadTime = 1000;
                    bulletDamage = 2;
                    maxAmmo = 6;
                    spriteSet = "bullet";
                    break;
                case GunType.SUPER:
                    recoverTime = 300;
                    reloadTime = 1000;
                    bulletDamage = 5;
                    maxAmmo = 100;
                    spriteSet = "superBullet";
                    break;
            }
            this.type = type;
            lastShoot = System.DateTime.Now;
            remainingAmmo = maxAmmo;
        }

        public void Fire(Level level, AmmoType ammoType, Vector2 position, FaceDirection direction)
        {
            if ((remainingAmmo > 0) && (lastShoot.AddMilliseconds(recoverTime).CompareTo(System.DateTime.Now) < 0))
            {
                position.X += direction == FaceDirection.Left ? -30 : 30;
                position.Y -= Tile.Height * 1.1f;
                Ammo ammo = new Ammo(level, ammoType, position, spriteSet, direction);
                ammo.Damage = bulletDamage;
                lastShoot = System.DateTime.Now;
                remainingAmmo--;
            }
        }

        public void Reset()
        {
            updateType(type);
        }

        public void Reload()
        {
            remainingAmmo = maxAmmo;
            lastShoot = DateTime.Now.Add(TimeSpan.FromMilliseconds(reloadTime));
        }
    }
}
